/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.changeidentifiers.testing.display.graphs;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import phd.changeidentifiers.testing.display.TestDisplayApplication;
import phd.changeidentifiers.testing.display.frames.GraphPanel;
import phd.changeidentifiers.testing.display.objects.TimeStepDBObject;

/**
 *
 * @author Max
 */
public class GraphTimeStepTimeDifferences extends GraphPanel{
    private TimeStepDBObject[] timeSteps;
    private TimeStepDBObject[] timeStepsWithoutCI;
    private int redrawCount;
    public GraphTimeStepTimeDifferences(
            TimeStepDBObject[] timeSteps,
            TimeStepDBObject[] timeStepsWithoutCI, int redrawCount){
        this.timeSteps = timeSteps;
        this.timeStepsWithoutCI = timeStepsWithoutCI;
        this.redrawCount = redrawCount;
        long maxTimeTaken = 0l;
        for(TimeStepDBObject timeStep: timeSteps){
            if(timeStep.getTimeTaken()>maxTimeTaken)maxTimeTaken = timeStep.getTimeTaken();
        }
        for(TimeStepDBObject timeStep: timeStepsWithoutCI){
            if(timeStep.getTimeTaken()>maxTimeTaken)maxTimeTaken = timeStep.getTimeTaken();
        }
        setXRange(0, this.timeSteps.length);
        setYRange(0, (int)maxTimeTaken);
        setScaleY(5);
        setScaleX(10);
        setxAxisTitle("Time Steps");
        setyAxisTitle("Time Taken (ms)");

    }
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 =  (Graphics2D) g;
        Point point;
        Point previousPoint = null;
        Point pointWithoutCI;
        Point previousPointWithoutCI = null;
        long timeTaken;
        long timeTakenWithoutCI;
        Stroke plain = new BasicStroke((TestDisplayApplication.LARGE?3f:1f));
        Stroke dashed = new BasicStroke((TestDisplayApplication.LARGE?3f:1f), BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1f, new float[]{25f, 5f, 5f, 5f}, 0f);
        g2.setStroke(plain);
        for(int x = 0 ;x < getMaxX(); x++){
            timeTaken = timeSteps[x].getTimeTaken();
            timeTakenWithoutCI = timeStepsWithoutCI[x].getTimeTaken();
            point = getScreenPosition(new Point(x, (int)timeTaken));
            g2.setColor((TestDisplayApplication.GRAYSCALE?Color.BLACK:Color.red.darker()));
            g2.fillRect(point.x-(TestDisplayApplication.LARGE?8:3),
                        point.y-(TestDisplayApplication.LARGE?8:3),
                        (TestDisplayApplication.LARGE?16:6),
                        (TestDisplayApplication.LARGE?16:6));
            if(previousPoint!=null){
                g2.setColor((TestDisplayApplication.GRAYSCALE?Color.DARK_GRAY:Color.red));
                g2.drawLine(previousPoint.x, previousPoint.y, point.x, point.y);
            }
            previousPoint = point;
            pointWithoutCI = getScreenPosition(new Point(x, (int)timeTakenWithoutCI));
            g2.setColor((TestDisplayApplication.GRAYSCALE?Color.DARK_GRAY:Color.blue.darker()));
            g2.drawOval(pointWithoutCI.x-(TestDisplayApplication.LARGE?8:3),
                        pointWithoutCI.y-(TestDisplayApplication.LARGE?8:3),
                        (TestDisplayApplication.LARGE?16:6),
                        (TestDisplayApplication.LARGE?16:6));
            if(previousPointWithoutCI!=null){
                g2.setColor((TestDisplayApplication.GRAYSCALE?Color.BLACK:Color.blue));
                g2.setStroke(dashed);
                g2.drawLine(previousPointWithoutCI.x, previousPointWithoutCI.y, pointWithoutCI.x, pointWithoutCI.y);
                g2.setStroke(plain);
            }
            previousPointWithoutCI = pointWithoutCI;

        }
        Point redrawPoint = getScreenPosition(2, -4);
        g2.drawString("Redrew: "+redrawCount, redrawPoint.x, redrawPoint.y);
    }

}
